package com.itheima.admin.contorller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.exception.MyException;
import com.itheima.common.result.JsonResult;
import com.itheima.entity.Setmeal;
import com.itheima.entity.SetmealDish;
import com.itheima.entity.dto.SetmealDto;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : sky
 * @date : 2023/3/5 21:54
 * @modyified By :
 */
@RestController
@RequestMapping("setmeal")
@ApiOperation(value = "菜品管理")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    @PostMapping
    @ApiOperation(value = "添加套餐")
    @CacheEvict(key = "setmealCache",allEntries = true)
    public JsonResult addSetmeal(@RequestBody SetmealDto setmealDto) {
        //添加套餐
        //封装setmeal对象
        Setmeal setmeal = new Setmeal();
        //转移数据
        BeanUtil.copyProperties(setmealDto, setmeal);
        setmealService.save(setmeal);
        //添加套餐内菜品
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishList();
        //添加setmealid
        List<SetmealDish> list = setmealDishList.stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;
        }).collect(Collectors.toList());
        //批量插入
        setmealDishService.saveBatch(list);
        //返回前端
        return JsonResult.success(setmealDto);
    }

    @ApiOperation(value = "修改套餐")
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public JsonResult updateSetmeal(@RequestBody SetmealDto setmealDto) {
        //修改套餐
        //封装对象
        Setmeal setmeal = new Setmeal();
        BeanUtil.copyProperties(setmealDto, setmeal);
        setmealService.updateById(setmeal);
        //删除原套餐的菜品信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getDishId, setmealDto.getId());
        setmealDishService.remove(wrapper);
        //套餐内增加新菜品
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishList();
        //添加套餐id
        List<SetmealDish> list = setmealDishList.stream().map(setmealDish -> {
            setmealDish.setDishId(setmealDto.getId());
            return setmealDish;
        }).collect(Collectors.toList());
          //批量添加套餐内菜品
        setmealDishService.saveBatch(list);
        //返回前端
        return JsonResult.success("修改成功！");
            }
    /***删除套餐
     *
     * @return*/
    @DeleteMapping
    @ApiOperation(value = "删除套餐")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public JsonResult delSetmeal(String ids){
        String[] split = ids.split(",");
        //id字符串转数组
        List<String> idlist = Arrays.asList(split);
        //判断套餐是否存在
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,idlist);
        Setmeal setmeal = setmealService.getOne(wrapper);
        if (null!=setmeal){
            //存在不能删除
            throw new MyException(0, "套餐存在，不能删除菜品");
        }
        //不存在，删除
        //1.1删除套餐信息
        setmealService.removeBatchByIds(idlist);
        //1.2删除套餐内菜品
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getSetmealId,idlist);
        setmealDishService.remove(wrapper1);
        //返回前端
        return JsonResult.success("删除套餐成功！");
    }
    /**
     * 套餐分页查询
     *
     * @return
     */
@ApiOperation(value = "套餐分页显示")
@GetMapping("page")
@Cacheable(value = "setmealCache",key = "#name")
    public JsonResult getSetmaelByPage(String name,
                                       Integer page,
                                       Integer pageSize){
    LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
    wrapper.like(Setmeal::getName,name);
    Page<Setmeal> setmealPage = new Page<>(page, pageSize);
    Page<Setmeal> page1 = setmealService.page(setmealPage, wrapper);
    return JsonResult.success(page1);
}
/**
 * 套餐上架？下架
 *
 * @return
 */
@PostMapping("{status}")
@ApiOperation(value = "套餐起售/停售")
public JsonResult changeSetmealStatus(String ids, @PathVariable String staus){
    //分割字符串
    String[] split = ids.split(",");
    //数组--->集合
    List<String> IDS = Arrays.asList(split);
    /**集合操作
     * */
    for (String id : IDS) {
        //通过id获得对象
        Setmeal setmeal = setmealService.getById(id);
        //设置状态码
        setmeal.setStatus(staus);
        //修改套餐信息
        setmealService.updateById(setmeal);
    }
    /**数组操作*/
    //for (int i = 0; i < split.length; i++) {
    //   String setmealId= split[i];
    //    Setmeal setmeal = setmealService.getById(setmealId);
    //    setmeal.setStatus(staus);
    //    setmealService.updateById(setmeal);
    //}
    return JsonResult.success("修改状态成功！");
}
/**
 * 根据id查询套餐
 *
 * @return
 */
@GetMapping("{id}")
@ApiOperation(value = "通过id查询套餐")
@Cacheable(value= "setmealCache",key = "#id")
public JsonResult getSetmealById(@PathVariable String id){
    Setmeal setmeal = setmealService.getById(id);
    if (null==setmeal){
        throw  new MyException(404, "id错误，套餐不存在！");
    }
    return JsonResult.success(setmeal);
}
}
